import csv
import pandas as pd
from tqdm import tqdm
import numpy as np
import os
from sklearn import preprocessing

def read_data(file_path):
    # file_path = "G:\\粤港澳数模\\附件：相关股票数据\\000028.SZ.xlsx"
    df_file_data = pd.read_excel(file_path)
    return df_file_data


def fun(df_file_data, stock_name):
    df = df_file_data
    print(df)
    temp1 = 0.0
    temp2 = 0
    temp3 = 0.0
    sumlist = []
    numlist = []
    avglist = []
    for index, row in tqdm(df.iterrows()):
        sum = row['成交额（元）'] - temp1
        num = row['成交量（手）'] - temp2
        temp1 = row['成交额（元）']
        temp2 = row['成交量（手）']
        sumlist.append(sum)
        numlist.append(int(num))
        if sum == 0.0 or num == 0:
            if temp3 == 0.0:
                avglist.append(row['收盘价'])
            else:
                avglist.append(temp3)
        else:
            temp3 = round(float(sum / (num * 100)), 2)
            avglist.append(temp3)


    df['当前额'] = sumlist
    df['当前量'] = numlist
    df['价'] = avglist
    print(df)
    df.to_excel("G:\\粤港澳数模\\data_aug_2\\{}.xlsx".format(stock_name))


if __name__ == "__main__":
    stock_dic = {'002027': '分众传媒', '000069': '华侨城Ａ', '002233': '塔牌集团', '300014': '亿纬锂能',
                 '600383': '金地集团', '002060': '粤水电',
                 '002475': '立讯精密', '600048': '保利地产', '002352': '顺丰控股', '000636': '风华高科',
                 '001914': '招商积余', '002511': '中顺洁柔',
                 '002449': '国星光电', '601318': '中国平安', '002303': '美盈森', '600183': '生益科技',
                 '600323': '瀚蓝环境', '002461': '珠江啤酒',
                 '000049': '德赛电池', '002152': '广电运通', '600872': '中炬高新', '002138': '顺络电子',
                 '000921': '海信家电', '600332': '白云山',
                 '300115': '长盈精密', '002035': '华帝股份', '000513': '丽珠集团', '600325': '华发股份',
                 '000651': '格力电器', '000028': '国药一致',
                 '601788': '光大证券', '000333': '美的集团', '002594': '比亚迪'
                 }
    stock_name_list = []
    file_name_list = os.listdir('G:\\粤港澳数模\\附件：相关股票数据')
    print(len(file_name_list))
    for i in file_name_list:
        stock_name = stock_dic[i[0:6]] + i[0:9]
        print(stock_name)
        stock_name_list.append(stock_name)
        file_path = 'G:\\粤港澳数模\\附件：相关股票数据\\' + i
        df_file_data = read_data(file_path)
        fun(df_file_data, stock_name)

